/*
**	Command & Conquer Red Alert(tm)
**	Copyright 2025 Electronic Arts Inc.
**
**	This program is free software: you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation, either version 3 of the License, or
**	(at your option) any later version.
**
**	This program is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/***************************************************************************
 **   C O N F I D E N T I A L --- W E S T W O O D   A S S O C I A T E S   **
 ***************************************************************************
 *                                                                         *
 *                 Project Name : iff                                      *
 *                                                                         *
 *                    File Name : WRITEPCX.CPP                             *
 *                                                                         *
 *                   Programmer : Julio R. Jerez                           *
 *                                                                         *
 *                   Start Date : May 2, 1995                              *
 *                                                                         *
 *                  Last Update : May 2, 1995   [JRJ]                      *
 *                                                                         *
 *-------------------------------------------------------------------------*
 * Functions:                                                              *
 * int Save_PCX_File (char* name, GraphicViewPortClass& pic, char* palette)*
 *= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/

#include  <wwlib32.h>
#include "filepcx.h"
static void Write_Pcx_ScanLine ( int file_handle , int scansize , char * ptr );


/***************************************************************************
 * WRITE_PCX_FILE -- Write the data in ViewPort to a pcx file              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 * INPUT:  name is a NULL terminated string of the fromat [xxxx.pcx]			*
 *			  pic	 is a pointer to a GraphicViewPortClass or to a 				*
 *					 GraphicBufferClass holding the picture. 							*
 *        palette is a pointer the the memry block holding the color 		*														*
 *              palette of the picture.                                    *
 *                                                                         *
 * OUTPUT: FALSE  if the function fails zero otherwise             			*
 *                                                                         *
 * WARNINGS:                                                               *
 *                                                                         *
 * HISTORY:                                                                *
 *   05/04/1995 JRJ : Created.                                             *
 *=========================================================================*/

int Write_PCX_File (char* name, GraphicViewPortClass& pic, BYTE* palette )
{
  unsigned     i ;
  unsigned	   width ;
  int 	      file_handle ;
  int				VP_Scan_Line ;
  char	     * ptr ;
  RGB  		  * pal ;
  GraphicBufferClass	* Graphic_Buffer ;
  PCX_HEADER   header = { 10 , 5 , 1 , 8 , 0 , 0 , 319 , 199 ,
								  320 , 200 , { 0 } , 0 , 1 , 320 , 1 , {0} } ;

  // Open file name
  file_handle = Open_File ( name , WRITE ) ;
  if ( file_handle == ERROR ) return FALSE ;


  header.width  = pic.Get_Width() - 1 ;
  header.height = pic.Get_Height() - 1 ;
  header.byte_per_line = pic.Get_Width() ;
  Write_File ( file_handle, & header , sizeof (PCX_HEADER)) ;

  VP_Scan_Line = pic.Get_Width()	+ pic.Get_XAdd();
  Graphic_Buffer = pic.Get_Graphic_Buffer() ;
  ptr = ( char * ) Graphic_Buffer->Get_Buffer() ;
  ptr += ( (pic.Get_YPos() * VP_Scan_Line) + pic.Get_XPos() );
  for ( i = 0 ; i < header.height + 1 ; i ++ ) 
   	Write_Pcx_ScanLine ( file_handle , header.byte_per_line, ptr + i *VP_Scan_Line ) ;

  pal = ( RGB * ) palette ;
  for ( i = 0 ; i < 256 ; i ++ ) {
	 pal -> red <<= 2 ;
	 pal -> green <<= 2 ;
	 pal -> blue <<= 2 ;
	 pal ++ ;
  }
 i = 0x0c ;
 Write_File ( file_handle, & i , 1 ) ;
 Write_File ( file_handle, palette , 256 * sizeof (RGB) ) ;

 Close_File (file_handle) ;
 return 0 ;
}




/***************************************************************************
 * WRITE_PCX_SCANLINE -- function to write a single pcx scanline to a file *
 *                                                                         *
 *                                                                         *
 * INPUT:                                                                  *
 *                                                                         *
 * OUTPUT:                                                                 *
 *                                                                         *
 * WARNINGS:                                                               *
 *                                                                         *
 * HISTORY:                                                                *
 *   05/04/1995 JRJ : Created.                                             *
 *=========================================================================*/

#define	POOL_SIZE 2048 
#define WRITE_CHAR(x) {	\
								* file_ptr ++ = x ; \
							   if ( file_ptr >= & pool [ POOL_SIZE ]	) { \
								  Write_File ( file_handle, pool , POOL_SIZE ) ; \
								  file_ptr = pool ; \
							 }	}

void Write_Pcx_ScanLine ( int file_handle , int scansize , char * ptr )
{
  unsigned     i ;
  unsigned     rle ;
  unsigned     color ;
  unsigned		last ;
  char		  * file_ptr ;	
  char		 	pool [ POOL_SIZE ] ;

  file_ptr = pool ;
  last = * ptr ; 
  rle = 1 ;
  for ( i = 1 ; i < scansize ; i ++ ) {
     color = 0xff & * ++ ptr ;
	  if ( color == last ) {
		 rle ++ ;
		 if ( rle == 63 )	 {
				 WRITE_CHAR ( 255 ) ;
				 WRITE_CHAR ( color ) ;
				 rle = 0 ;
		 }
	  } else if ( rle ) {
		 if ( rle == 1 && ( 192 != ( 192 & last ))) {
			    WRITE_CHAR ( last ) ;
		 } else {
			    WRITE_CHAR ( rle + 192 ) ;
	   		 WRITE_CHAR ( last ) ;
		 }
		 last = color ;
		 rle = 1 ;
	  }
   }
	if ( rle ) {
		 if ( rle == 1 && ( 192 != ( 192 & last ))) {
			    WRITE_CHAR ( last ) ;
		 } else {
			    WRITE_CHAR ( rle + 192 ) ;
	   		 WRITE_CHAR ( last) ;
		 }
	}

	Write_File ( file_handle, pool , ( int ) file_ptr - ( int ) pool ) ; 
}


